home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / nvram.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-11  |  5.4 KB  |  386 lines

  1. /* Non-Volatile Ram user commands
  2.  * Copyright 1993 Brian A. Lantz, KO4KS
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "config.h"
  7. #include "mbuf.h"
  8. #include "internet.h"
  9. #include "timer.h"
  10. #include "netuser.h"
  11. #include "iface.h"
  12. #include "session.h"
  13. #include "ip.h"
  14. #include "cmdparse.h"
  15. #include "commands.h"
  16.  
  17. #define old
  18.  
  19. extern int32 Ip_addr;
  20. extern char Mycall[];
  21. #ifdef nope
  22. static int doipadd __ARGS((int argc,char *argv[],void *p));
  23. #endif
  24. static int doread __ARGS((int argc,char *argv[],void *p));
  25. static int dowrite __ARGS((int argc,char *argv[],void *p));
  26.  
  27. void PutNV __ARGS((int value));
  28. int readit __ARGS((int address, int single));
  29. int writeit __ARGS((int address, int value));
  30.  
  31. static struct cmds nvcmds[] = {
  32. #ifdef nope
  33.     "ipaddress",      doipaddr,       0,      0, NULLCHAR,
  34. #endif
  35.     "read",           doread,         0,      0, NULLCHAR,
  36. #ifndef old
  37.     "write",          dowrite,        0,      0, NULLCHAR,
  38. #endif
  39.     NULLCHAR,
  40. };
  41.  
  42. int
  43. donv(argc,argv,p)
  44. int argc;
  45. char *argv[];
  46. void *p;
  47. {
  48.     return subcmd(nvcmds,argc,argv,p);
  49. }
  50.  
  51. #ifdef nope
  52. static int
  53. doipaddr(argc,argv,p)
  54. int argc;
  55. char *argv[];
  56. void *p;
  57. {
  58. int32 n;
  59.  
  60.     if(argc < 2) {
  61.         tprintf("%s\n",inet_ntoa(Ip_addr));
  62.     } else if((n = resolve(argv[1])) == 0){
  63.         tprintf(Badhost,argv[1]);
  64.         return 1;
  65.     } else
  66.         Ip_addr = n;
  67.     return 0;
  68. }
  69.  
  70. #endif
  71.  
  72. static int
  73. doread(argc,argv,p)
  74. int argc;
  75. char *argv[];
  76. void *p;
  77. {
  78. #ifdef old
  79. char *n = 0x3f8;
  80. #endif
  81. int c;
  82. char buf[8], *newargv[3];
  83.  
  84. #ifdef old
  85.     c = *n;
  86. #else
  87.     if ((c = readit (0, 1)) == -1)    {
  88.         tprintf ("NV Read Error\x07\n");
  89.         return 0;
  90.     }
  91. #endif
  92.     Ip_addr += (c << 8);
  93.     sprintf (buf, "unit%-.2d", c);
  94.     setcall(Mycall,buf);
  95.     newargv[1] = buf;
  96.     newargv[0] = newargv[2] = NULLCHAR;
  97.     dohostname (2, newargv, (void *) 0);
  98.     tprintf ("NV Ram Read\n");
  99.     return 0;
  100. }
  101.  
  102.  
  103. #ifndef old
  104. static int
  105. dowrite(argc,argv,p)
  106. int argc;
  107. char *argv[];
  108. void *p;
  109. {
  110. int c;
  111.  
  112. /* takes unit # from Ip_addr, for now */
  113.  
  114.     c = ((Ip_addr >> 8) & 0x0000ff);
  115.     if (writeit (0, c))
  116.         tprintf ("NV Ram Write Error\x07\n");
  117.     else
  118.         tprintf ("NV Ram Write Complete\n");
  119.     return 0;
  120. }
  121.  
  122.  
  123. #asm
  124. Base    equ    $700000
  125. PACNT    equ    Base+$81e
  126. PADDR    equ    Base+$820
  127. PADAT    equ    Base+$822
  128. PBCNT    equ    Base+$824
  129. PBDDR    equ    Base+$826
  130. PBDAT    equ    Base+$828
  131.  
  132. Wait:    nop
  133.     nop
  134.     nop
  135.     dbra    d0,Wait
  136.     rts
  137.  
  138. GetNVAck:
  139.     bclr    #0,PBDDR+1    set it for input
  140.     move.l    #20,d0
  141.     bsr    Wait
  142.     bsr    CLKhi
  143.     move.l    #20,d0
  144.     bsr    Wait
  145.     move.b    PBDAT+1,d1
  146.     bset    #0,PBDDR+1    set it for output
  147.     move.l    #20,d0
  148.     bsr    Wait
  149.     bsr    CLKlo
  150.     btst    #0,d1
  151.     bne    GetAk1
  152. #ifdef DIAG
  153.     lea.l    ACK(pc),a0
  154.     move.l    a0,d0
  155.     bsr    puts
  156. #endif
  157.     clr.l    d0
  158.     rts
  159.  
  160. GetAk1
  161. #ifdef DIAG
  162.     lea.l    NOACK(pc),a0
  163.     move.l    a0,d0
  164.     bsr    puts
  165. #endif
  166.     move.l    #-1,d0
  167.     rts
  168.  
  169. DATAlo:    bclr    #0,PBDAT+1
  170.     move.l    #20,d0
  171.     bra    Wait
  172.  
  173. DATAhi:    bset    #0,PBDAT+1
  174.     move.l    #20,d0
  175.     bra    Wait
  176.  
  177. CLKlo:    bclr    #4,PADAT
  178.     move.l    #20,d0
  179.     bra    Wait
  180.  
  181. CLKhi:    bset    #4,PADAT
  182.     move.l    #20,d0
  183.     bra    Wait
  184.  
  185. #endasm
  186.  
  187. start ()
  188. {
  189. #ifdef DIAG
  190.     puts ("Start bit being sent");
  191. #endif
  192.     DATAhi();
  193.     CLKhi();
  194.     DATAlo();
  195.     CLKlo();
  196. #ifdef DIAG
  197.     puts ("Complete!");
  198. #endif
  199. }
  200.  
  201. stop ()
  202. {
  203. #ifdef DIAG
  204.     puts ("Stop bit being sent");
  205. #endif
  206.     DATAlo();
  207.     CLKhi();
  208.     DATAhi();
  209.     CLKlo();
  210. #ifdef DIAG
  211.     puts ("Complete!\n");
  212. #endif
  213. }
  214.  
  215. clockbit ()
  216. {
  217.     CLKhi();
  218.     Wait (20);
  219.     CLKlo();
  220. }
  221.  
  222. zero ()
  223. {
  224.     DATAlo();
  225. #ifdef DIAG
  226.     putchar ('0');
  227. #endif
  228.     clockbit();
  229. }
  230.  
  231. one ()
  232. {
  233.     DATAhi();
  234. #ifdef DIAG
  235.     putchar ('1');
  236. #endif
  237.     clockbit();
  238. }
  239.  
  240. void
  241. PutNV (value)
  242. int value;
  243. {
  244. int bitmask = 0x80, count;
  245.  
  246.     for (count = 0; count < 8; count++)    {
  247.         if (value & bitmask)
  248.             one();
  249.         else
  250.             zero();
  251.         bitmask >>= 1;
  252.     }
  253. }
  254.  
  255. int
  256. getbit()
  257. {
  258. #asm
  259.     move.b    PBDAT+1,d1
  260.     and.b    #$fe,d1
  261. #ifdef nope
  262.     move.l    #20,d0
  263.     bsr    Wait
  264. #endif
  265.     move.l    d1,d0
  266. #endasm
  267. }
  268.  
  269.  
  270. int
  271. GetNV ()
  272. {
  273. int bits, count;
  274. int value = 0;
  275.  
  276. @    bclr    #0,PBDDR+1    /* set it for input */
  277.     Wait(20);
  278.     for (count = 0; count < 8; count++)    {
  279.         CLKhi();
  280.         value >>= 1;
  281. /*        Wait (20);        */
  282.         bits = getbit();
  283.         if (bits)    {
  284. #ifdef DIAG
  285.             putchar ('1');
  286. #endif
  287.             value |= 0x80;
  288.         } else    {
  289. #ifdef DIAG
  290.             putchar ('0');
  291. #endif
  292.         }
  293.         CLKlo();
  294. /*        Wait (20);        */
  295.     }
  296. @    bset    #0,PBDDR+1    /* set it for output */
  297.     putchar ('\n');
  298.     return (value);
  299. }
  300.  
  301. int
  302. readit (address, single)
  303. int address, single;
  304. {
  305. int value;
  306.  
  307.     start();
  308.     PutNV (0xa0);
  309.     if (GetNVAck () == -1)
  310.         return (-1);
  311.     PutNV (address);
  312.     if (GetNVAck () == -1)
  313.         return (-1);
  314.     start();
  315.     PutNV (0xa1);
  316.     if (GetNVAck () == -1)
  317.         return (-1);
  318.     value = GetNV ();
  319.     if (single)
  320.         stop();
  321. #ifdef DIAG
  322.     printf ("Value of NV Ram at that location: %02x\n", value);
  323. #endif
  324.     return (value);
  325. }
  326.  
  327.  
  328. int
  329. writeit (address, value)
  330. int address;
  331. int value;
  332. {
  333.     start();
  334.     PutNV (0xa0);
  335.     if (GetNVAck () == -1)
  336.         return (-1);
  337.     PutNV (address);
  338.     if (GetNVAck () == -1)
  339.         return (-1);
  340.     PutNV (value);
  341.     if (GetNVAck () == -1)
  342.         return (-1);
  343.     stop();
  344.     return (0);
  345. }
  346.  
  347.  
  348.  
  349. #asm
  350. EnNV    bclr    #4,PACNT    make sure PA12 is enabled
  351.     bset    #4,PADDR    set it for output
  352.     bclr    #0,PBCNT+1    make sure that PB0 is enabled
  353.     bset    #0,PBDDR+1    set it for output
  354.     move.l    #20,d0
  355.     bra    Wait
  356.  
  357. #endasm
  358.  
  359. void
  360. AckIt()
  361. {
  362.     DATAlo ();
  363.     CLKhi ();
  364.     CLKlo ();
  365. }
  366.  
  367.  
  368. int
  369. readall (buf)
  370. unsigned char *buf;
  371. {
  372. int c;
  373.  
  374.     c = readit (0, 0);
  375.     if (c == -1)
  376.         return (-1);
  377.     buf[0] = c;
  378.     for (c = 1; c < 256; c++)    {
  379.         AckIt ();
  380.         buf[c] = GetNV();
  381.     }
  382.     stop ();
  383.     return (0);
  384. }
  385. #endif
  386.